/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * JFrameTaiKhoan.java
 *
 * Created on Oct 14, 2011, 6:20:36 PM
 */
package desktopapplicationdemo1;

import Database.TaiKhoan;
import java.util.Vector;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author SEVEN
 */
public class JFrameTaiKhoan extends javax.swing.JFrame {

    /** Creates new form JFrameTaiKhoan */
    public JFrameTaiKhoan() {
        initComponents();
        HienThiDSTK();
    }
    
    public void HienThiDSTK()
    {
        Vector<Object> cols = new Vector<Object>();
        cols.add("Username");
        cols.add("Password");
        cols.add("Ho Ten");
        cols.add("Quyen");
        DefaultTableModel dtm = new DefaultTableModel(TaiKhoan.DanhSachTaiKhoan(), cols){
            @Override
            public boolean isCellEditable(int row, int column) {
                return false;
            }            
        };
        this.jTableDSTK.setModel(dtm);    
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTableDSTK = new javax.swing.JTable();
        jPanel1 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jTextFieldUsername = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jTextFieldHoten = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jButtonThem = new javax.swing.JButton();
        jComboBoxQuyen = new javax.swing.JComboBox();
        jButtonCapNhat = new javax.swing.JButton();
        jButtonXoa = new javax.swing.JButton();
        jButtonClear = new javax.swing.JButton();
        jPasswordFieldPassword = new javax.swing.JPasswordField();

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(desktopapplicationdemo1.DesktopApplicationDemo1.class).getContext().getResourceMap(JFrameTaiKhoan.class);
        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setName("Form"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        jTableDSTK.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        jTableDSTK.setName("jTableDSTK"); // NOI18N
        jTableDSTK.getTableHeader().setResizingAllowed(false);
        jTableDSTK.getTableHeader().setReorderingAllowed(false);
        jTableDSTK.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTableDSTKMouseClicked(evt);
            }
        });
        jTableDSTK.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                jTableDSTKKeyReleased(evt);
            }
        });
        jScrollPane1.setViewportView(jTableDSTK);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)), resourceMap.getString("jPanel1.border.title"), javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, resourceMap.getFont("jPanel1.border.titleFont"))); // NOI18N
        jPanel1.setName("jPanel1"); // NOI18N

        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        jTextFieldUsername.setEditable(false);
        jTextFieldUsername.setText(resourceMap.getString("jTextFieldUsername.text")); // NOI18N
        jTextFieldUsername.setName("jTextFieldUsername"); // NOI18N

        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N

        jTextFieldHoten.setName("jTextFieldHoten"); // NOI18N

        jLabel4.setText(resourceMap.getString("jLabel4.text")); // NOI18N
        jLabel4.setName("jLabel4"); // NOI18N

        jLabel5.setText(resourceMap.getString("jLabel5.text")); // NOI18N
        jLabel5.setName("jLabel5"); // NOI18N

        jButtonThem.setText(resourceMap.getString("jButtonThem.text")); // NOI18N
        jButtonThem.setName("jButtonThem"); // NOI18N
        jButtonThem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonThemActionPerformed(evt);
            }
        });

        jComboBoxQuyen.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "User", "Admin" }));
        jComboBoxQuyen.setName("jComboBoxQuyen"); // NOI18N

        jButtonCapNhat.setText(resourceMap.getString("jButtonCapNhat.text")); // NOI18N
        jButtonCapNhat.setName("jButtonCapNhat"); // NOI18N
        jButtonCapNhat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonCapNhatActionPerformed(evt);
            }
        });

        jButtonXoa.setText(resourceMap.getString("jButtonXoa.text")); // NOI18N
        jButtonXoa.setName("jButtonXoa"); // NOI18N
        jButtonXoa.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonXoaActionPerformed(evt);
            }
        });

        jButtonClear.setText(resourceMap.getString("jButtonClear.text")); // NOI18N
        jButtonClear.setName("jButtonClear"); // NOI18N
        jButtonClear.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonClearActionPerformed(evt);
            }
        });

        jPasswordFieldPassword.setText(resourceMap.getString("jPasswordFieldPassword.text")); // NOI18N
        jPasswordFieldPassword.setName("jPasswordFieldPassword"); // NOI18N

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(30, 30, 30)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTextFieldUsername, javax.swing.GroupLayout.PREFERRED_SIZE, 295, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jLabel3)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                            .addComponent(jLabel5)
                            .addGap(18, 18, 18)
                            .addComponent(jComboBoxQuyen, 0, 297, Short.MAX_VALUE))
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                            .addComponent(jLabel4)
                            .addGap(18, 18, 18)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addGroup(jPanel1Layout.createSequentialGroup()
                                    .addComponent(jButtonClear)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(jButtonThem)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(jButtonCapNhat)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(jButtonXoa))
                                .addComponent(jTextFieldHoten, javax.swing.GroupLayout.DEFAULT_SIZE, 295, Short.MAX_VALUE)
                                .addComponent(jPasswordFieldPassword)))))
                .addContainerGap(65, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jTextFieldUsername, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jPasswordFieldPassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(jTextFieldHoten, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(jComboBoxQuyen, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButtonThem)
                    .addComponent(jButtonCapNhat)
                    .addComponent(jButtonXoa)
                    .addComponent(jButtonClear))
                .addContainerGap(22, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(25, 25, 25)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING))
                .addContainerGap(36, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 108, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(29, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jTableDSTKMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTableDSTKMouseClicked
        LayThongTin();        
    }//GEN-LAST:event_jTableDSTKMouseClicked

    private void jTableDSTKKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTableDSTKKeyReleased
        LayThongTin();            
    }//GEN-LAST:event_jTableDSTKKeyReleased

    private void jButtonCapNhatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonCapNhatActionPerformed
        
        int index = this.jTableDSTK.getSelectedRow();
        TaiKhoan tk = new TaiKhoan();
        tk.setPassword(String.valueOf(this.jPasswordFieldPassword.getPassword()));
        tk.setHoten(this.jTextFieldHoten.getText());
        tk.setQuyen(this.jComboBoxQuyen.getSelectedItem().toString());
        if(tk.CapNhatTaiKhoan(index))
        {
            JOptionPane.showMessageDialog(null, "Cap nhat thanh cong");
            HienThiDSTK();
        }
        else
            JOptionPane.showMessageDialog(null, "Cap nhat thay bai");
        
    }//GEN-LAST:event_jButtonCapNhatActionPerformed

    private void jButtonXoaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonXoaActionPerformed
        
        int op = JOptionPane.showConfirmDialog(null, "Ban co chac xoa tai khoan nay khong?", "Xac nhan", JOptionPane.OK_CANCEL_OPTION);
        if(op==JOptionPane.OK_OPTION)
        {
            String username = this.jTextFieldUsername.getText();
            TaiKhoan tk = new TaiKhoan();
            if(tk.XoaTaiKhoan(username))
            {
                JOptionPane.showMessageDialog(null, "Xoa tai khoan thanh cong");
                HienThiDSTK();
            }
            else
                JOptionPane.showMessageDialog(null, "Xoa tai khoan that bai");
        }
        
        
    }//GEN-LAST:event_jButtonXoaActionPerformed

    private void jButtonClearActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonClearActionPerformed
        
        this.jTextFieldUsername.setEditable(true);
        this.jTextFieldUsername.setText("");
        this.jPasswordFieldPassword.setText("");
        this.jTextFieldHoten.setText("");
        this.jComboBoxQuyen.setSelectedIndex(0);
        
    }//GEN-LAST:event_jButtonClearActionPerformed

    private void jButtonThemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonThemActionPerformed
        
        TaiKhoan tk = new TaiKhoan();
        tk.setUsername(this.jTextFieldUsername.getText());
        tk.setPassword(String.valueOf(this.jPasswordFieldPassword.getPassword()));
        tk.setHoten(this.jTextFieldHoten.getText());
        tk.setQuyen(this.jComboBoxQuyen.getSelectedItem().toString());
        if(tk.ThemTK())
        {
            JOptionPane.showMessageDialog(null, "Them tai khoan thanh cong");
            HienThiDSTK();
        }
        else
            JOptionPane.showMessageDialog(null, "Them tai khoan that bai");
        
    }//GEN-LAST:event_jButtonThemActionPerformed

    private void LayThongTin()
    {
        String username = this.jTableDSTK.getValueAt(this.jTableDSTK.getSelectedRow(), 0).toString();
        TaiKhoan tk = new TaiKhoan();
        tk = tk.ThongTinTaiKhoan(username);
        this.jTextFieldUsername.setText(tk.getUsername());
        this.jTextFieldHoten.setText(tk.getHoten());
        int index = tk.getQuyen().toLowerCase().trim().equals("user") ? 0 : 1;
        this.jComboBoxQuyen.setSelectedIndex(index);
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(JFrameTaiKhoan.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(JFrameTaiKhoan.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(JFrameTaiKhoan.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(JFrameTaiKhoan.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new JFrameTaiKhoan().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonCapNhat;
    private javax.swing.JButton jButtonClear;
    private javax.swing.JButton jButtonThem;
    private javax.swing.JButton jButtonXoa;
    private javax.swing.JComboBox jComboBoxQuyen;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPasswordField jPasswordFieldPassword;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTableDSTK;
    private javax.swing.JTextField jTextFieldHoten;
    private javax.swing.JTextField jTextFieldUsername;
    // End of variables declaration//GEN-END:variables
}
